package com.cskaoyan.service.impl;

import cn.hutool.core.convert.Convert;
import com.cskaoyan.bean.CskaoyanmallUser;
import com.cskaoyan.bean.CskaoyanmallUserExample;
import com.cskaoyan.bean.vo.UserListVo;
import com.cskaoyan.mapper.CskaoyanmallUserMapper;
import com.cskaoyan.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author Fantasylkh
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    CskaoyanmallUserMapper userMapper;

    @Override
    public UserListVo queryUserList(String username, String mobile,Integer page, Integer limit, String sort, String order) {

//        CskaoyanmallUserExample userExample = new CskaoyanmallUserExample();
//        userExample.setOrderByClause(sort + " " + order);
//        List<CskaoyanmallUser> users = userMapper.selectByExample(userExample);
//
//        PageHelper.startPage(page, limit);
//
//        PageInfo<CskaoyanmallUser> userPageInfo = new PageInfo<>(users);
//        long total = userPageInfo.getTotal();
//
//        UserListVo userListVo = new UserListVo();
//        userListVo.setItems(users);
//        userListVo.setTotal(total);
//        return userListVo;

        PageHelper.startPage(page, limit);
        CskaoyanmallUserExample userExample = new CskaoyanmallUserExample();
        CskaoyanmallUserExample.Criteria criteria = userExample.createCriteria();


        if (!ObjectUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!ObjectUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        if (!ObjectUtils.isEmpty(sort) && !ObjectUtils.isEmpty(order)) {
            userExample.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(false);
        List<CskaoyanmallUser> users = userMapper.selectByExample(userExample);
        PageInfo<CskaoyanmallUser> userPageInfo = new PageInfo<>(users);
        long total = userPageInfo.getTotal();

        UserListVo userListVo = new UserListVo();
      userListVo.setTotal(total);
      userListVo.setItems(users);

        return userListVo;
    }


    @Override
    public Integer count() {
        CskaoyanmallUserExample example = new CskaoyanmallUserExample();
        example.or().andDeletedEqualTo(false);
        long l = userMapper.countByExample(example);
        return Convert.toInt(l);
    }

    @Override
    public CskaoyanmallUser selectUserByName(String principal) {
        CskaoyanmallUser user = null;
        if (!ObjectUtils.isEmpty(principal)) {
            user = userMapper.selectOneByUserName(principal);
        }
        return user;
    }

    @Override
    public CskaoyanmallUser updateUserInfo(CskaoyanmallUser user) {
        userMapper.updateByPrimaryKeySelective(user);
        return user;
    }

    @Override
    public CskaoyanmallUser selectAllById(Integer userId) {
        CskaoyanmallUser cskaoyanmallUser = userMapper.selectByPrimaryKey(userId);
        return cskaoyanmallUser;
    }
}
